#!/usr/bin/python3
import sys

class Board:
    def __init__(self):
        self.matrix = []
        self.marks = []
        self.columns = 0
        self.winner = False
        self.last_draw = 0

    def __str__(self):
        return '\n'.join([ '\t'.join(str(e) for e in row) for row in self.matrix ])

    def decode(self, line):
        elements = ' '.join(line.split()).split(' ')
        self.columns = len(elements)
        self.matrix.append([ [int(e), False] for e in elements ])

    def mark(self, draw):
        self.last_draw = draw
        for row in self.matrix:
            for element in row:
                if draw == element[0]:
                    element[1] = True

    def row_check(self, i):
        for j in range(self.columns):
            if not self.matrix[j][i][1]:
                return False
        return True

    def column_check(self, i):
        for j in range(self.columns):
            if not self.matrix[i][j][1]:
                return False
        return True

    def check_win(self):
        possible_wins = [ True for e in range(self.columns) ]
        for i in range(self.columns):
            horizontal = self.row_check(i)
            vertical = self.column_check(i)
            if (not horizontal) and (not vertical):
                possible_wins[i] = False
                continue
        for i in range(self.columns):
            if possible_wins[i]:
                self.winner = True

    def final_score(self):
        score = 0
        for row in self.matrix:
            for element in row:
                if not element[1]:
                    score += element[0]
        return score * self.last_draw

def someone_wins(draws, boards):
    for draw in draws:
        print('drawing', draw)
        # three passes, because there can be multiple winners
        for board in boards:
            board.mark(draw)
        for board in boards:
            board.check_win()
        for board in boards:
            if board.winner:
                return True
    return False

draws = [ int(e) for e in input().split(',') ]
boards = []
for line in sys.stdin:
    if line == '\n':
        boards.append(Board())
        continue
    boards[-1].decode(line.rstrip('\n'))

if someone_wins(draws, boards):
    for board in boards:
        if board.winner:
            print('board has won:\n', board)
            print('final score:', board.final_score())
